//进行路由初始化和配置
//导入
import VueRouter from 'vue-router'

import auth from '@/utils/auth'
//注册
import Vue from 'vue'
import Login from '@/views/login'
import Home from '@/views/home'
import Welcome from '@/views/welcome'
import NotFound from '@/views/404'


Vue.use(VueRouter)
//导入组件
//初始化
//路由实例
const router = new VueRouter({

  routes: [
    { path: '/login', component: Login },
    {
      path: '/', component: Home,
      children: [
        { path: '/', component: Welcome }
      ]
    },
    // 通配规则（以上所有的规则不符合的时候，走这个规则）
    { path: '*', component: NotFound }
  ]//路由规则
})
  //前置导航守卫
  router.beforeEach((to, from, next) => {
    if (to.path !== '/login' && !auth.getUser().token)
      return next('/login')
    //其他情况下放行
      next()
  })
//导出路由实例
export default router